package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c.entity.*;
import com.c.service.*;
import com.c.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.sql.Wrapper;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangxiangli
 * @create 2023-02-24-17:04
 */
@RequestMapping("/OrderForm")
@RestController
@CrossOrigin
public class OrderFormController {

    @Autowired
    private OrderFormService orderFormService;


    @Autowired
    private OrderSettingService orderSettingService;


    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private UserService userService;

    /**
     * 订单分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderFormService.pageQuery2(queryPageBean);
        return pageResult;
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo")
    public Result getOrderInfo(String id) {
        OrderForm orderForm = orderFormService.getOrderInfoById(id);
        List<Setmeal> setmeals = setmealService.list();
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("remark","健康管理师");
        List<User> users = userService.list(wrapper);
        Map data = new HashMap();
        data.put("orderForm", orderForm);
        data.put("setmeals", setmeals);
        data.put("users",users);
        return new Result(true, MessageConstant.QUERY_COMMONSUCCESS, data);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderForm orderForm) {
        try {
            orderFormService.updateById(orderForm);
            return new Result(true, MessageConstant.COMMON_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_EDIT_FAILE);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderFormService.removeById(id);
            return new Result(true, MessageConstant.COMMON_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_DELETE_FAILE);
        }
    }


}